<?php
/*单点登录接口
if (!class_exists("go_sql")){
include("conf/mlogin.le.je");
  $le=new go_sql();
  $le->connect(); 
}
include("conf/fun_login.php");*/

class servlogin {
	public $err1="101"; //无授权，拒绝连接
	public $err2="102"; //传输数据类型出错
	public $err3="103"; //传输数据格式出错
	public $err4="104"; //数据无法传输
	public $err5="105"; //数据为空值或数据异常
	public $err6="106"; // 数据服务出错，无法提交订单
	public $err7="107"; // 返回数据非XML格式嘻嘻嘻
	public $err8="108"; // 网络中断
	public $err9="109"; // 重复查询
	public $err10="110"; // 重复提交
	
	public $err501="501"; // 用户名不能为空或含有非法字符
	public $err502="502"; // 密码不能为空或含有非法字符
	public $err503="503"; // 用户名错误  
	public $err504="504"; // 账户还未激活不能登录	
	public $err505="505"; // 密码错误
	public $err506="506"; // 密码错误禁止登录1小时
	
	public $err601="601"; // 票据残缺
	public $err602="602"; // 无此票据
	public $err603="603"; // 票据已失效
	public $err604="604"; // 此用户已被管理员禁止登录
	
	public $err701="701"; // 手机号已被使用
	public $err702="702"; // 密码格式错误
	public $err703="703"; // 用户ID错误
	public $err704="704"; // 请输入正确的手机号码
	public $err705="705"; // 验证码长度不对
	public $err706="706"; // 找不到此手机号相关记录

//站点连接身份验证
	public function authrize($ac, $pw){
		$ac=urlencode($ac);
		//$pw=urlencode($pw);
	
	    $str="select id from idex where account=\"".$ac."\" and pwd=\"".$pw."\"";
        $GLOBALS['le']->query($str);
	    if ($GLOBALS['le']->fetch_assoc()) {
			return true;
		}else{
			return false;
		}
	}

//登录接口
	public function Member_Login_Post($ac, $pw, $name, $pwd) {
	
		if ($this->authrize($ac, $pw)){
		if (p_match($name)) return $this->err501;
		if (p_match($pwd)) return $this->err502;
		
	  	$str="select id,pwd,dlogin,uname,act,unix_timestamp(cz_time) as ltime from idmember where (mobile=\"".$name."\" or passport=\"".$name."\") and jz='0' limit 1";
		
		//return $str;
      	$GLOBALS['le']->query($str);
      	$GLOBALS['le']->fetch_assoc();
      	$id=$GLOBALS['le']->f("id");
	  	$password=$GLOBALS['le']->f("pwd");
	  	$dlogin=$GLOBALS['le']->f("dlogin");
	  	$uname=$GLOBALS['le']->f("uname");
	  	$act=$GLOBALS['le']->f("act");
		$ltime=$GLOBALS['le']->f("ltime");
		$now=$_SERVER['REQUEST_TIME'];
				
		$a1=array();
		
		if (!$id) return $this->err503;
		if ($act!='0') return $this->err504;
		if ($ltime && ($now-$ltime)<3600) return $this->err506;
		if ($password!=$pwd) {
			if ($dlogin>=5) {
				$str="update idmember set cz_time=now()  where id=$id limit 1";
				$GLOBALS['le']->query($str);
			}else{
				$str="update idmember set dlogin=dlogin+1  where id=$id limit 1";
				$GLOBALS['le']->query($str);				
			}
			return $this->err505;
		}
							   
	    //登录成功，生成票据返回
		/*票据生成简介
		1、30分钟没动作销毁
		2、退出即销毁
		3、应用列表内必须传递，否则视为不登录
		*/
		//清空错误记录
		$str="update idmember set cz_time=0,dlogin=0,last_time=now() where id=$id limit 1";
		$GLOBALS['le']->query($str);
		//生成票据号
		$str="select password(concat(md5(id),unix_timestamp(last_time))) as tcode from idmember where id=$id";
		$GLOBALS['le']->query($str);
      	$GLOBALS['le']->fetch_assoc();
      	$tcode=$GLOBALS['le']->f("tcode");		
		
		$a1[]=array("code"=>$tcode);
		//更新票据时间戳
		$tt=$_SERVER['REQUEST_TIME'];
		$str="update idmember set tcode='$tcode',ttime='$tt' where id=$id limit 1";
		$GLOBALS['le']->query($str);		
		return toXML($a1);
		
		}else{
		return $this->err1;
		}

    }

//检验登录
	public function Member_Check_List($ac, $pw, $tcode) {
		if ($this->authrize($ac, $pw)){
		if (strlen($tcode)!=41) return $this->err601;
		$tcode=htmlspecialchars($tcode);
		//检查
		$str="select id,jz,ttime from idmember where tcode='$tcode' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if (!$r['id']) return $this->err602;
		if ($r['jz']==1) return $this->err604;
		$nt=$_SERVER['REQUEST_TIME'];
		$ct=$nt-$r['ttime'];
		if ($ct>3600*30) return $this->err603;
		
		
        $a1=array();
		$str="select id,passport,uname,nick,sex,head,person_info as pinfo,address,mobile,tel,email,lvl,act,last_time as ltime,money,coupon from idmember where id=".$r['id']." limit 1";
		//return $str;
		$GLOBALS['le']->query($str);
      	$a1[]=$GLOBALS['le']->fetch_assoc();
		return toXML($a1);
		}else{
		return $this->err1;
		}	
	}

//退出登录
	public function Member_Login_Out($ac, $pw, $tcode) {
		if ($this->authrize($ac, $pw)){
		if (strlen($tcode)!=41) return $this->err601;
		$tcode=htmlspecialchars($tcode);
		//检查
		$str="select id,ttime from idmember where tcode='$tcode' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if (!$r['id']) return $this->err602;
		
        $a1=array();
		$str="update idmember set ttime=0 where id=".$r['id'];
		$GLOBALS['le']->query($str);
      	$a1[]=array("done"=>"SUCCESS");
		return toXML($a1);
		}else{
		return $this->err1;
		}	
	}

//注册用户
	public function Member_Register($ac, $pw, $mobile, $pwd, $uname, $nick, $sex, $email, $person_info, $address, $tel, $coupon=0) {
		if ($this->authrize($ac, $pw)){
		//检查
		if (!preg_match('/^1(3|5|8|4|6|7)\d{9}$/ ',$mobile)) return $this->err704;
		$str="select id from idmember where mobile='$mobile' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if ($r['id']) return $this->err701;
		if (strlen($pwd)!=32) return $this->err702;
		
		//生成通行证
		$pp=$_SERVER['REQUEST_TIME'];
		$pp1=substr($pp,0,5);
		$pp2=substr($pp,5,5);
		$zf="ABCDEFGHJKT";
		$zf_n=rand(0,9);
		$pp="X".$pp1.$zf{$zf_n}.$pp2;
		
        $a1=array();
		$str="insert idmember set ";
		$str.="passport='".$pp."',";
		$str.="mobile     ='".$mobile     ."',            
			   pwd        ='".$pwd        ."',            
			   uname      ='".$uname      ."',            
               nick       ='".$nick       ."',            
               sex        ='".$sex        ."',            
               email      ='".$email      ."',            
               person_info='".$person_info."',            
               address    ='".$address    ."',            
               tel        ='".$tel        ."',
			   coupon     ='".$coupon     ."',
			   last_time  =now(),
			   create_time=now()";
		
		//return $str;
		$GLOBALS['le']->query($str);
		$id=mysql_insert_id();
		//生成票据号
		$str="select password(concat(md5(id),unix_timestamp(last_time))) as tcode from idmember where id=$id";
		$GLOBALS['le']->query($str);
      	$GLOBALS['le']->fetch_assoc();
      	$tcode=$GLOBALS['le']->f("tcode");		
		
		//更新票据时间戳
		$tt=$_SERVER['REQUEST_TIME'];
		$str="update idmember set tcode='$tcode',ttime='$tt' where id=$id limit 1";
		$GLOBALS['le']->query($str);		
		
		
      	$a1[]=array("done"=>"SUCCESS","code"=>$tcode);
		return toXML($a1);
		}else{
		return $this->err1;
		}	
	}

//更新用户数据
	public function Member_Info_Update($ac, $pw, $id, $mobile, $pwd, $uname, $nick, $head, $sex=0, $email, $person_info, $address, $tel, $act=0) {
		if ($this->authrize($ac, $pw)){
		//检查
		$str="select id from idmember where mobile='$mobile' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if ($r['id']) return $this->err701;
		if ($pwd && strlen($pwd)!=32) return $this->err702;

		$str="select id from idmember where id='$id' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if (!$r['id']) return $this->err703;

        $a1=array();
		$str="update idmember set ";
		if ($pwd) $str.="pwd='".$pwd."',";
		if ($uname) $str.="uname='".$uname."',";
		if ($nick) $str.="nick='".$nick."',";
		if ($head) $str.="head='".$head."',";
		$str.="sex='".$sex."',";
		if ($email) $str.="email='".$email."',";
		if ($person_info) $str.="person_info='".$person_info."',";
		if ($address) $str.="address='".$address."',";
		if ($tel) $str.="tel='".$tel."',";
		$str.="act='".$act."'";

		$str.=" where id='$id' limit 1";
		$GLOBALS['le']->query($str);

      	$a1[]=array("done"=>"SUCCESS");
		return toXML($a1);
		}else{
		return $this->err1;
		}	
	}

//忘记密码
	public function Member_Forget_Pwd($ac, $pw, $mobile, $dcode) {
		if ($this->authrize($ac, $pw)){
		if (!preg_match('/^1(3|5|8|4|6|7)\d{9}$/ ',$mobile)) return $this->err704;
		$dlen=strlen(htmlspecialchars($dcode));
		if ($dlen!=6) return $this->err705;
		//检查
		require_once("tel/tel.php");
		
		$str="select id from idmember where mobile='$mobile' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if (!$r['id']) return $this->err706;
		
		//验证短信发送
		$_w['sms_pw']="31cfc170f70535b0d4dc92363c486de8";
		$sendmsg="鼓浪屿商务网密码验证码：".$dcode;

		$x_ac=10;
    	$url="http://gateway.woxp.cn:6630/utf8/web_api/?x_eid=10286&x_uid=Denny&x_pwd_md5=".$_w['sms_pw']."&x_ac=".$x_ac."&x_gate_id=300&x_target_no=".$mobile."&x_memo=".$sendmsg; 
		$num=Get($url);
		
        $a1=array();
		if ($num>0){
      	$a1[]=array("done"=>"SUCCESS");
		}else{
		$error=telcode($num);
		$a1[]=array("error"=>$error);	
		}
		return toXML($a1);
		}else{
		return $this->err1;
		}	
	}

//修改密码
	public function Member_Modify_Pwd($ac, $pw, $mobile, $newPwd) {
		if ($this->authrize($ac, $pw)){
		if (!preg_match('/^1(3|5|8|4|6|7)\d{9}$/ ',$mobile)) return $this->err704;
		$dlen=strlen(htmlspecialchars($newPwd));
		if ($dlen!=32) return $this->err705;
		//检查
		require_once("tel/tel.php");
		
		$str="select id from idmember where mobile='$mobile' limit 1";
		$GLOBALS['le']->query($str);
      	$r=$GLOBALS['le']->fetch_assoc();
		if (!$r['id']) return $this->err706;
		
		//修改密码
		$str="update idmember set pwd='".$newPwd."' where id=".$r['id']." limit 1";
		$GLOBALS['le']->query($str);		
		
      	$a1[]=array("done"=>"SUCCESS");
		return toXML($a1);
		}else{
		return $this->err1;
		}	
	}

}

//$kk=new service;
//echo $kk->Get_ScenicSpot_Info(3);
//ini_set("soap.wsdl_cache_enabled","0");
/*$server=new SoapServer("SloginMX.wsdl",array('encoding' =>'UTF-8','cache_wsdl' => 0));
$server->setClass("servlogin");
$server->handle();*/
?> 